package com.crazy.crazybusinessservice.controller.inner;

import com.crazy.crazybusinessservice.service.BankCardBalanceRecordService;
import com.crazy.crazybusinessservice.service.BankCardBalanceService;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazymodel.model.dto.trade.TradeRequest;
import com.crazy.crazymodel.model.entity.BankCardBalance;
import com.crazy.crazymodel.model.entity.BankCardWaterRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/inner/bankcard")
@Slf4j
public class BankCardBalanceInnerController {

    @Resource
    private BankCardBalanceService balanceService;
    @Resource
    private BankCardBalanceRecordService recordService;

    /**
     * 使用银行卡号进行支付
     * @param tradeRequest
     * @return
     */
    @PostMapping("/pay")
    public String pay(@RequestBody TradeRequest tradeRequest){

        String msg = balanceService.pay(tradeRequest.getBankCardId(),tradeRequest.getAmount());
        return msg;
    }

    /**
     * 按银行卡号进行充值
     * @param tradeRequest
     * @return
     */
    @PostMapping("/revenue")
    public BaseResponse<String> revenue(@RequestBody TradeRequest tradeRequest){
        String msg = balanceService.revenue(tradeRequest.getBankCardId(),tradeRequest.getAmount());
        if(msg.equals("not find cardId")){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR,msg);
        }else{
            recordService.addRecord(tradeRequest.getBankCardId(),tradeRequest.getAmount(),0);
            return ResultUtils.success(msg);
        }
    }

    /**
     * 按银行卡号进行赎回
     * @param tradeRequest
     * @return
     */
    @PostMapping("/redeem")
    public BaseResponse<String> redeem(@RequestBody TradeRequest tradeRequest){
        String msg = balanceService.revenue(tradeRequest.getBankCardId(),tradeRequest.getAmount());
        if(msg.equals("not find cardId")) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR, msg);
        }else if(msg.equals("redeem amount error")){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR,msg);
        }else {
            msg = "redeem success";
            recordService.addRecord(tradeRequest.getBankCardId(),tradeRequest.getAmount(),0);
            return ResultUtils.success(msg);
        }
    }

    /**
     * 撤单时调用接口，按金额数将冻结金额解冻
     * @param tradeRequest
     * @return
     */
    @PostMapping("orderChancel")
    public BaseResponse<String> freezeBalanceChancel(@RequestBody TradeRequest tradeRequest){
        String msg = balanceService.freezeBalanceChancel(tradeRequest.getBankCardId(),tradeRequest.getAmount());
        if(msg.equals("not find cardId")) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR, msg);
        }else if(msg.equals("chancel amount error")){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR,msg);
        }else {
            return ResultUtils.success(msg);
        }
    }

    /**
     * 对所有银行卡进行冻结金额清算
     *
     * @return
     */
    @PostMapping("/liquidate")
    public BaseResponse<String> liquidate(){
        List<BankCardBalance> balanceList = balanceService.list();
        String msg = null;
        for(BankCardBalance balance: balanceList){
            msg =  balanceService.liquidate(balance.getBankCardNumber());
            if(!msg.equals("liquidate success")){
                break;
            }
        }
        if(!msg.equals("liquidate success")){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR,msg);
        }
        return ResultUtils.success(msg);
    }

    /**
     * 根据银行卡号进行冻结金额的清算
     * @param
     * @return
     */
    @PostMapping("/liquidateById")
    public BaseResponse<String> liquidateById(@RequestBody TradeRequest tradeRequest){
        String msg = null;
        msg = balanceService.liquidate(tradeRequest.getBankCardId(),tradeRequest.getAmount());
        if(!msg.equals("liquidate success")){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR,msg);
        }
        recordService.addRecord(tradeRequest.getBankCardId(),tradeRequest.getAmount(),1);
        return ResultUtils.success(msg);
    }


}
